package ru.geyes.screw.collections;

import java.util.Collection;

/**
 * @author alex
 *         date 31.05.2008
 *         time 17:42:01
 */
public class TypedArrayList<T> extends ArrayListX<T> implements TypedList<T> {
    private Class<T> oType;

    public TypedArrayList(int initialCapacity, Class<T> classType) {
        super(initialCapacity);
        this.oType = classType;
    }

    public TypedArrayList(Class<T> classType) {
        super();
        this.oType = classType;
    }

    public TypedArrayList(Collection<? extends T> c, Class<T> classType) {
        super(c);
        this.oType = classType;
    }

    public boolean add(T t) {
        if (t != null && !oType.isAssignableFrom(t.getClass()))
            throw new IllegalArgumentException("Your class has incorrect type");  
        return super.add(t);
    }

    public void add(int index, T element) {
        if (element != null && !oType.isAssignableFrom(element.getClass()))
            throw new IllegalArgumentException("Your class has incorrect type");
        super.add(index, element);
    }

    public Class<T> getContentClass() {
        return oType;
    }
}
